<?php
class kcorder_statControl extends skymvc{
	
	public function __construct(){
		parent::__construct();
	}
	
	public function onDefault(){
		$day=get('day','h');
		if(!$day){
			$day=date("Y-m-d");
		}
		$start=strtotime($day);
		$end=$start+3600*24;
		$where="  status=3 AND dateline>=$start AND dateline<$end  ";
		$oids=m("kcorder")->selectCols(array(
			"where"=> $where,
			"fields"=>"order_id"
		));
		if($oids){
			$sql="select productid,sum(price) as price,sum(gys_price) as gys_price,sum(buy_num) as buy_num  
				from ".table('kcorder_product')." 
				where order_id in("._implode($oids).")  group by productid
			";	
			$data=M("kcorder_product")->getAll($sql);
			if($data){
				foreach($data as $v){
					$ids[]=$v['productid'];
				}
				$products=M("product")->getListByIds($ids);
				foreach($data as &$v){
					$v['title']=$products[$v['productid']]['title'];
				}
			}
		}
		$this->smarty->assign(array(
			"data"=>$data,
			"day"=>$day
		));
		
		$this->smarty->display("kcorder_stat/index.html");
		
	}
	
	public function onMonth(){
		$day=get('day','h');
		if(!$day){
			$day=date("Y-m");
		}
		$start=strtotime($day);
		$end=strtotime("+1 month",$start);
		$where="  status=3 AND dateline>=$start AND dateline<$end  ";
		$oids=m("kcorder")->selectCols(array(
			"where"=> $where,
			"fields"=>"order_id"
		));
		if($oids){
			$sql="select productid,sum(price) as price,sum(gys_price) as gys_price,sum(buy_num) as buy_num  
				from ".table('kcorder_product')." 
				where order_id in("._implode($oids).")  group by productid
			";	
			$data=M("kcorder_product")->getAll($sql);
			if($data){
				foreach($data as $v){
					$ids[]=$v['productid'];
				}
				$products=M("product")->getListByIds($ids);
				foreach($data as &$v){
					$v['title']=$products[$v['productid']]['title'];
				}
			}
		}
		$this->smarty->assign(array(
			"data"=>$data,
			"day"=>$day
		));
		
		$this->smarty->display("kcorder_stat/month.html");
		
	}
	
}
?>